package states;

import gui.OpCom;
import regulators.BallAndBeamRegul;
import regulators.BeamRegul;
import se.lth.control.realtime.DigitalIn;
import se.lth.control.realtime.DigitalOut;
import se.lth.control.realtime.IOChannelException;
import monitor.StateVariableMonitor;

public class Beam extends StateClass implements State {

	public Beam(DigitalIn initialPosSensor, DigitalOut digitalSolenoid, BeamRegul beam,
			BallAndBeamRegul ballAndBeam, OpCom op, StateVariableMonitor stateVariableMonitor) {
		super(initialPosSensor, digitalSolenoid, beam, ballAndBeam, op, stateVariableMonitor);
	}

	double refDeltaBeam = 0.005;
	double initialReference = 1;

	@Override
	public void initialExcecution() {
		op.displayStep("Beam");
		if(!beam.isRunning()) {
			beam.restart();
		}
	}

	@Override
	public void excecute() {
		beam.setRef(initialReference);
		initialReference -= refDeltaBeam;
	}

	@Override
	public boolean stateOfExcecution() {

		try {
			return !initialPosSensor.get();
		} catch (IOChannelException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public void nextState() {
		setCurrentState(new ReleaseBall(initialPosSensor, digitalSolenoid, beam, ballAndBeam, op, stateVariableMonitor));
	}


	@Override
	public void suspend() {
		beam.shutDown();	
	}

	@Override
	public void resume() {
		beam.restart();	
	}



}